<?php
namespace app\admin\controller;

class DeptManage extends Common
{
	private $dept_manage;
	public function __construct(){
		parent::__construct();
		$this -> dept_manage = db('dept_manager');

	}
	public function index()
	{
		if (request() -> isPost())
        {
            //$key      = input('post.key','', 'trim');
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit')?input('limit'):config('pageSize');
            $list     = $this -> dept_manage
            		 -> alias('m')
            		 -> join('wqs_classify c', 'c.id=m.classify_id', 'left')
            		 -> join('wqs_dept d', 'd.id=m.dept_id', 'left')
                     -> field('m.*,c.name c_name,d.name d_name')
                     //-> where('p.number|l.title','like',"%".$key."%")
                     //-> where('s.title_id','like',"%".$key."%")
                     -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                     -> toArray();
            
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
		return view();
	}

	public function add()
	{
		if (request()->isPost()) {
			//验证密码
            $password   = input('post.password', '', 'trim');
            $repwd      = input('post.repwd', '', 'trim');
            if ($password != $repwd) {
            	return ['code' => 0, 'msg'  => '两次密码不一致', 'url'  => url('add')];
            }

            //添加数据
            $data       	  = input('post.');
            $data['password'] = md5($password);

            if($this -> dept_manage->where(array('username'=>$data['username']))->find()){
                return $result = ['code' => 0, 'msg'  => '用户名重复，添加部门管理员失败！', 'url'  => url('add')];
            }

            $result     = $this -> dept_manage -> insert($data);
            if ($result) {
            	$result = ['code' => 1, 'msg'  => '添加部门管理员成功', 'url'  => url('index')];
            } else {
            	$result = ['code' => 0, 'msg'  => '添加部门管理员失败', 'url'  => url('add')];
            }
            // cache('adList', null);
            return $result;
        }
		$classifyList = db('wqs_classify')->where(['open' => 1])->order('sort')->select();
        $this->assign('classifyList', $classifyList);
		return view();
	}

	public function edit()
	{
		if (request()->isPost()) {
			//验证密码
            $data       = input('post.');
            $password   = input('post.password', '', 'trim');
            $repwd      = input('post.repwd', '', 'trim');
            if ($password != $repwd) {
            	return ['code' => 0, 'msg'  => '两次密码不一致', 'url'  => url('add')];
            }

            if (!$password) {
            	unset($data['password']);
            } else {
           		$data['password'] = md5($password);
            }

            //添加数据
            $data['ip'] = request()->ip();
            $result     = $this -> dept_manage -> update($data);
            if ($result !== false) {
            	$result = ['code' => 1, 'msg'  => '修改部门管理员成功', 'url'  => url('index')];
            } else {
            	$result = ['code' => 0, 'msg'  => '修改部门管理员失败', 'url'  => url('add')];
            }
            // cache('adList', null);
            return $result;
        }
		$id = input('param.id');
		$list1 = $this -> dept_manage -> find($id);
		$this -> assign('list1', $list1);
		$deptList     = db('wqs_dept')    ->where(['classify_id'=>$list1['classify_id'],'open'=>1])->select();
		$classifyList = db('wqs_classify')->where(['open' => 1])->order('sort')->select();
        $this->assign('classifyList', $classifyList);
        $this->assign('deptList', $deptList);
		return view();
	}


    public function del()
    {
        $id   = input('param.id');
        $info = $this -> dept_manage -> delete($id);
        if ($info)
        {
            return ['code'=>1,'msg'=>'删除成功！'];
        } else 
        {
            return ['code'=>1,'msg'=>'删除失败！'];
        }        
    
    }

 	public function status()
    {
        $id     = input('post.id');
        $status = $this-> dept_manage -> where(array('id'=>$id))->value('status');
        if($status==1)
        {
            $data['status'] = 0;
            $this->dept_manage->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 0;
            $result['status']  = 1;
        }else
        {
            $data['status'] = 1;
            $this->dept_manage->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 1;
            $result['status'] = 1;
        }
        return $result;
    }


	public function getDeptJoin($id = 0)
    {

        if (empty($id)) {
            $result = array(
                'code' => 0,
                'msg'  => 'error:参数为空',
            );
            return $result;
        }
        $where = [
            'classify_id' => $id,
            'open'        => 1,
        ];
        try {
            $dept = db('wqs_dept');
            $data = $dept
                ->field('id,name')
                ->where($where)
                ->order('sort')
                ->select();
            $result = [
                'code' => 1,
                'msg'  => '获取成功!',
                'data' => $data,
            ];
        } catch (Exception $e) {
            $result = [
                'code' => -1,
                'msg'  => 'error:服务器异常',
            ];
        }
        return $result;
    }
}